﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace CrazyWorship1
{


    public class ScreenList : ObservableCollection<Screen>, IXmlSerializable
    {
        public ScreenList()
        {
        }

        public Screen Add(Guid id, string deviceName, string clientUrl, Rectangle bounds)
        {
            Screen s = new Screen(id, deviceName, clientUrl, bounds);
            Add(s);
            return s;
        }
        public Screen Add(Guid id, string deviceName, string clientUrl)
        {
            Screen s = new Screen(id, deviceName, clientUrl);
            Add(s);
            return s;
        }
        public bool HasScreen(ScreenInfo s)
        {
            foreach (Screen screen in this)
            {
                if ((screen.DeviceName == s.DeviceName) && (screen.ClientUrl == s.ClientUrl))
                    return true;
            }
            return false;
        }
        public static ScreenList Load()
        {
            FileStream stream = new FileStream(ClientManager.screenFile, FileMode.OpenOrCreate);
            XmlSerializer x = new XmlSerializer(typeof(ScreenList));
            ScreenList si =new ScreenList();
            try
            {
                si = (ScreenList)x.Deserialize(stream);
            }
            catch(Exception e)
            {
                Console.Write(e.Message);
            }
                stream.Close();
            return si;
        }
        public void Save()
        {
            XmlSerializer x = new XmlSerializer(typeof(ScreenList));
            FileStream stream = new FileStream(ClientManager.screenFile, FileMode.Create);
            x.Serialize(stream, this);
            stream.Close();
            //ds.WriteXml(clientsFile);
        }
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement("ScreenList"); 
            reader.ReadStartElement("ScreenList");
            if (reader.NodeType != XmlNodeType.EndElement)
            {
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    Screen c = new Screen();
                    c.ReadXml(reader);
                    this.Add(c);
                }
            }

            //    reader.ReadEndElement();
            //  reader.MoveToContent();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("ScreenList");
            foreach (Screen info in this)
            {
                ((IXmlSerializable)info).WriteXml(writer);
            }
            writer.WriteEndElement();

        }

        #endregion
    }
}
